﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation.Provider;
using System.Management.Automation;
using System.Collections.ObjectModel;

namespace Obra.PowerShell.SmugMug
{
    class PropertyCmdletProvider<TSmugMug> :
        IPropertyCmdletProvider
    {
        private readonly CmdletProvider cmdletProvider;
        private readonly TSmugMug smugmug;

        /// <summary>
        /// c-tor
        /// </summary>
        public PropertyCmdletProvider(
            CmdletProvider cmdletProvider,
            TSmugMug smugmug)
        {
            this.cmdletProvider = cmdletProvider;
            this.smugmug = smugmug;
        }

        #region IPropertyCmdletProvider Members

        public void ClearProperty(string path, System.Collections.ObjectModel.Collection<string> propertyToClear)
        {
            throw new NotImplementedException();
        }

        public object ClearPropertyDynamicParameters(string path, System.Collections.ObjectModel.Collection<string> propertyToClear)
        {
            return new DynamicParametersItem();
        }

        /// <summary>
        /// Add a PSNoteProperty to the specified PSObject.
        /// </summary>
        private void AddPSNotePropertyToPSObject(
            PSObject propertyValue,
            string name)
        {
            try
            {
                PSMemberInfo memberInfo = PSObject.AsPSObject(this.smugmug).Properties[name];
                if (memberInfo != null)
                {
                    propertyValue.Properties.Add(new PSNoteProperty(name, memberInfo.Value));
                }
            }
            catch (GetValueException e)
            {
                this.cmdletProvider.WriteError(new ErrorRecord(
                    e,
                    "GetValueError",
                    ErrorCategory.ReadError,
                    name));
            }
        }

        /// <summary>
        /// Get the property or properties of the SmugMug type.
        /// </summary>
        public void GetProperty(string path, Collection<string> providerSpecificPickList)
        {
            if (providerSpecificPickList == null || providerSpecificPickList.Count == 0)
            {
                this.cmdletProvider.WritePropertyObject(PSObject.AsPSObject(this.smugmug), path);
            }
            else
            {
                PSObject propertyValue = new PSObject();
                providerSpecificPickList
                    .Where(
                        x => !String.IsNullOrEmpty(x)).ToList()
                    .ForEach(
                        x => this.AddPSNotePropertyToPSObject(propertyValue, x));

                this.cmdletProvider.WritePropertyObject(propertyValue, path);
            }
        }

        public object GetPropertyDynamicParameters(string path, System.Collections.ObjectModel.Collection<string> providerSpecificPickList)
        {
            return new DynamicParametersItem();
        }

        public void SetProperty(string path, System.Management.Automation.PSObject propertyValue)
        {
            throw new NotImplementedException();
        }

        public object SetPropertyDynamicParameters(string path, System.Management.Automation.PSObject propertyValue)
        {
            return new DynamicParametersItem();
        }

        #endregion
    }
}
